<?php

namespace App\Http\Controllers\Innerend;



use App\Models\User;
use App\Models\UserAgent;

class UserAgentController extends BaseController
{

    // 用户
    public function initialize()
    {
        parent::initialize();

        $this->model = 'UserAgent';
    }


    // 列表
    // 添加
    // 修改
    // 删除


    // 审核
    public function onVerify()
    {
        $manage = $this->checkManage($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $result = $manage->onVerify();
        if($result === false){
            return $this->error($manage->getError());
        }

        return $this->success([], '更新成功！');
    }


    // 列表数据展示
    protected function listTable($lists)
    {
        // 整理
        $trList = [];
        if(empty($lists)){
            return $trList;
        }

        // 展示列表表格
        $sexes   = [1 => '男', 2 => '女', 0 => '保密'];
        $verifys = $this->verifys();
        //$types    = (new UserAgent())->getType(false);
        $listHtml = $this->adminList();
        foreach($lists as $key => $item){
            // 数据
            $_id = $item['id'];
            //$status   = $item['status'] == 1 ? '正常' : '禁用';
            //$statusOp = $item['status'] == 1 ? '禁用' : '启用';
            // 操作
            $operate = '';
            //$operate .= $listHtml->operate_status(['id' => $_id, 'value' => 1 - $item['status']], $statusOp, $item['status']);

            // 审核操作
            if($item['verify'] == 0){
                $operate .= $listHtml->operate_status(['id' => $_id, 'value' => 1], '审核通过', 1, 'on-verify');
                $operate .= $listHtml->operate_status(['id' => $_id, 'value' => 2], '审核不通过', 2, 'on-verify');
            }
            $operate .= $listHtml->operate_edit(['id' => $_id], '编辑认证');
            //$operate .= $listHtml->operate_soft_delete(['id' => $_id], '你确定要删除该用户吗？');

            // 页面
            $tr = $listHtml->list_check($item['id']);
            $tr .= $listHtml->list_td($item['agent_name']);
            $tr .= $listHtml->list_td($item['telephone']);
            //$tr .= $listHtml->list_td($types[$item['agent_type']] ?? '');
            //$tr .= $listHtml->list_date($item['form_time']);
            //$tr .= $listHtml->list_date($item['end_time']);
            //$tr .= $listHtml->list_td($item['is_pay'] == 1 ? '是' : '否');
            //$tr .= $listHtml->list_td($item['is_propose'] == 1 ? '是' : '否');
            $tr .= $listHtml->list_td($verifys[$item['verify']] ?? '-');
            $tr .= $listHtml->list_td(date('Y-m-d H:i', $item['ctime']));
            $tr .= $listHtml->list_td($operate);

            // 整合
            $trList[] = $listHtml->list_tr($tr);
        }

        // 整合
        return $trList;
    }

    // 列表顶部按钮
    protected function listTopButton()
    {
        $addUrl     = Url('/innerend/user-agent/add');
        $top_button = '<a href="javascript:;" onclick="onEdit(\''.$addUrl.'\')" class="btn btn-success radius">添加认证</a> ';
        $this->assign('top_button', $top_button);
    }

    // 列表字段
    protected function list_fields()
    {
        // 'agent_type' => '认证类型','form_time' => '开始时间','end_time' => '结束时间','is_pay' => '是否支付','is_propose' => '是否推荐',
        return ['agent_name' => '认证名称', 'telephone' => '电话', 'verify' => '状态', 'ctime' => '创建时间', 'operate' => '操作',];
    }

    // 列表搜索
    protected function list_search()
    {
        //$model   = new UserAgent();
        //$tyeps   = $model->getType(false);
        $request = $this->optLogic()->getData();
        $form    = $this->adminList();

        $verify    = $form->formSelect('verify', $request['verify'] ?? null, $this->verifys());
        $isPropose = $form->formSelect('is_propose', $request['is_propose'] ?? null, ['1' => '推荐', 0 => '未推荐']);
        //$agent_type = $form->formSelect('agent_type', isset($request['agent_type']) ? ($tyeps[$request['agent_type']] ?? null) : null, $tyeps);
        $agent_name = $form->formInput('agent_name', $request['agent_name'] ?? null, '认证名称');
        $mobile     = $form->formInput('mobile', $request['mobile'] ?? null, '手机号');
        $search     = ['verify' => $verify, 'is_propose' => $isPropose, 'agent_name' => $agent_name,]; // 'agent_type' => $agent_type,
        $this->assign('search', $search);

        $searchHide = ['channel'];
        $this->assign('searchHide', []);
    }

    // 表单页面
    protected function formTable()
    {
        $manage = $this->checkManage($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $html = '';
        $info = $manage->onShow();
        $form = $this->adminForm();

        // 用户
        $uInfo   = (new User())->isCache(true)->getOneById($info['uid'] ?? 0);
        $verifys = $this->verifys();
        //$types   = (new UserAgent())->getType();

        // 用户名
        //$uid          = $form->ajaxSearch('uid', $info['uid'] ?? '', 'User');
        $uid    = $form->ajaxSingleFromList('uid', $uInfo, 'User', 'id', 'username');
        $status = $form->formRadio('status', $info['status'] ?? 1, [1 => '正常', 0 => '禁用']);

        //$agent_type   = $form->formRadio('agent_type', $info['agent_type'] ?? '', $types);
        $agent_name   = $form->formInput('agent_name', $info['agent_name'] ?? '', '认证名称');
        $city         = $form->formInput('city', $info['city'] ?? '', '城市');
        $address      = $form->formInput('address', $info['address'] ?? '', '地址');
        $grade        = $form->formInput('grade', $info['grade'] ?? '', '学校等级');
        $description  = $form->formTextarea('description', $info['description'] ?? '');
        $license_img  = $form->formImage('license_img', $info['license_img'] ?? '');
        $school_allow = $form->formImage('school_allow', $info['school_allow'] ?? '');
        $is_pay       = $form->formRadio('is_pay', $info['is_pay'] ?? 1, [1 => '是', 0 => '否']);
        $verify       = $form->formRadio('verify', $info['verify'] ?? 1, $verifys);
        $_id          = $form->formHidden('id', $info['id'] ?? '');

        //$html     .= $form->makeItem('确认密码', $password, '添加时候不填默认密码123456，修改不填默认不修改');
        //$html .= $form->makeItem('状态', $status);
        $html .= $form->makeItem('用户', $uid);
        //$html .= $form->makeItem('认证类型', $agent_type);
        $html .= $form->makeItem('认证名称', $agent_name);
        $html .= $form->makeItem('城市', $city);
        $html .= $form->makeItem('地址', $address);
        $html .= $form->makeItem('学校等级', $grade);
        $html .= $form->makeItem('营业执照', $license_img);
        $html .= $form->makeItem('办学许可', $school_allow);
        $html .= $form->makeItem('是否支付', $is_pay);
        $html .= $form->makeItem('审核状态', $verify);
        $html .= $form->makeItem('认证描述', $description);
        $html .= $_id;

        return $html;
    }

    // 审核状态
    protected function verifys()
    {
        return [2 => '审核未通过', 1 => '审核通过', 0 => '未审核',];
    }
}


